package com.kurumi.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.kurumi.bo.Page;
import com.kurumi.bo.PageData;
import com.kurumi.bo.PageParam;
import com.kurumi.pojo.Store;
import com.kurumi.service.PharmacyService;
import com.kurumi.util.JsonUtil;

@Controller
@RequestMapping("/pharmacy")
public class PharmacyController {

	@Autowired
	private PharmacyService pharmacyService;

	/**
	 * 跳转到药房信息页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/map")
	public ModelAndView map() throws Exception {
		Map<String, Object> model = new HashMap<String, Object>();
		return new ModelAndView("/pharmacy/map", model);
	}

	/**
	 * 跳转到药房信息页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/toPharmacyListPage")
	public ModelAndView toAdminUserListPage() throws Exception {
		Map<String, Object> model = new HashMap<String, Object>();
		return new ModelAndView("/pharmacy/pharmacyListPage", model);
	}

	@ResponseBody
	@RequestMapping(value = "/getPharmacyList")
	public void getPharmacyList(HttpServletRequest request, HttpServletResponse response, PageParam pageParam)
			throws Exception {
		Page pager = new Page();
		pager.setPage(pageParam.getPage());
		pager.setRecPerPage(pageParam.getLimit());

		PageData<Map<String, Object>> pd = new PageData<Map<String, Object>>();
		PageInfo<Map<String, Object>> pageInfo = pharmacyService.getPharmacyPageInfo(request, pageParam);

		List<Map<String, Object>> list = pageInfo.getList();

		if (list != null && list.size() > 0) {
			pd.setCount((int) pageInfo.getTotal());
			pd.setData(list);
		} else {
			pd.setCount(0);
			pd.setData(new ArrayList<Map<String, Object>>());
		}

		String json = JsonUtil.objectToJson(pd);
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			out.print(json);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 跳转到药房信息添加信息
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/pharmacyAddlPage")
	public ModelAndView printDetailPage(Integer orderId) throws Exception {
		Map<String, Object> model = new HashMap<String, Object>();

		Store store = pharmacyService.getPharmacyById(orderId);
		model.put("store", store);
		if(store!=null) {
			model.put("status", "update");
		}

		return new ModelAndView("/pharmacy/pharmacyAddlPage", model);
	}

	@ResponseBody
	@RequestMapping(value = "/saveStore")
	public void saveStore(HttpServletRequest request, HttpServletResponse response, Store store) throws Exception {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out;

		JSONObject jsonObject = new JSONObject();

		try {
			store.setIsSynchronized(1);
			pharmacyService.saveOrUpdateStore(store);

			jsonObject.put("code", "1");
			jsonObject.put("msg", "操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			jsonObject.put("code", "0");
			jsonObject.put("msg", "操作失败");
		} finally {
			out = response.getWriter();
			out.print(jsonObject.toString());
			out.flush();
			out.close();
		}
	}
	
	
	
	@ResponseBody
	@RequestMapping(value = "/delStoreById")
	public void delStoreById(HttpServletRequest request, HttpServletResponse response, Integer id)
			throws Exception {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out;

		JSONObject jsonObject = new JSONObject();

		try {

			pharmacyService.delStoreById(id);

			jsonObject.put("code", "1");
			jsonObject.put("msg", "操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			jsonObject.put("code", "0");
			jsonObject.put("msg", "操作失败");
		} finally {
			out = response.getWriter();
			out.print(jsonObject.toString());
			out.flush();
			out.close();
		}
	}
	
	
	

}
